package com.ironone.streaming;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SlidingPaneLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.ironone.streaming.application.CusAlrtBtnTypes;
import com.ironone.streaming.application.CusAlrtIconTypes;
import com.ironone.streaming.application.CustomAlertDialog;
import com.ironone.streaming.application.FormObject;
import com.ironone.streaming.application.MainApplication;

public class WeeklyPickFragment extends Fragment {
	View view;
	SlidingPaneLayout mPane;
	Button btnMenu;

	ImageView albumart_bg;
	Bitmap bitmap;
	
	@Override
	public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.weeklypick_fragment, container, false);
	
		try {
			mPane = (SlidingPaneLayout) getActivity().findViewById(R.id.pane);
			btnMenu = (Button) view.findViewById(R.id.btnMenu);
			btnMenu.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					mPane.openPane();
				}
			});
			
			//Menu btn
			int supWidth = (int)((FormObject.getInstance().getScreenWidth() * 0.6));
			RelativeLayout.LayoutParams abParam = (RelativeLayout.LayoutParams) btnMenu.getLayoutParams();
			abParam.width = (int)(supWidth - (supWidth * 0.88));
			abParam.height = (int)(supWidth - (supWidth * 0.88));
			btnMenu.setLayoutParams(abParam);
			
			//Background overlay image
			bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg_frag);
			albumart_bg = (ImageView) view.findViewById(R.id.albumart_bg);
			ViewTreeObserver vto = albumart_bg.getViewTreeObserver();

			vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
				@SuppressWarnings("deprecation")
				@Override
				public void onGlobalLayout() {
					albumart_bg = (ImageView) view.findViewById(R.id.albumart_bg);
					if(FormObject.getInstance().getAlbumArtImage() == null)
						albumart_bg.setImageBitmap(bitmap);
					else 
						albumart_bg.setImageBitmap(MainApplication.getByteArrToBitmap(FormObject.getInstance().getAlbumArtImage()));
					
					albumart_bg.setAlpha(64);
				}
			});
			
		} catch (Exception e) {
			CustomAlertDialog alertDialog = new CustomAlertDialog(getActivity(), CusAlrtBtnTypes.OK, CusAlrtIconTypes.DUCK);
			alertDialog.setTitleText(getString(R.string.what_the_duck_));
			alertDialog.setMessageText(getString(R.string.something_snapped));			
			alertDialog.show();
		}
		return view;
	}
}
